// app.js
'use strict';
const CircuitBreaker = require('./app/lib/circuit_breaker');

class AppBootHook {
  constructor(app) {
    this.app = app;

    // 在 beforeStart 生命周期中访问 redis
    this.app.beforeStart(async () => {
      // 初始化熔断器
      this.app.circuitBreaker = new CircuitBreaker(app.config.circuitBreaker);
    });
  }
  async willReady() {
    this.app.logger.info('🚀 开始应用初始化...');
    await this.testRedisConnection(this.app);
  }

  async testRedisConnection(app) {
    // const { app } = this;

    if (!app.redis) {
      app.logger.error('❌ Redis 客户端未初始化，请检查 plugin.js 和 config.default.js');
      return;
    }

    try {
      await app.redis.set('ping', 'pong');
      const result = await app.redis.get('ping');
      app.logger.info(`✅ Redis 连接测试成功: ${result}`);
    } catch (e) {
      app.logger.error('❌ Redis 连接失败:', e.message);
    }
  }
}

module.exports = AppBootHook;